<?php


namespace App\Server;


use App\Models\SendCode\UserSendCode;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class Sms
{
    /**
     * 返回状态码
     * @var string[]
     */
    private $statusStr = array(
        "0" => "短信发送成功",
        "-1" => "参数不全",
        "-2" => "服务器空间不支持,请确认支持curl或者fsocket，联系您的空间商解决或者更换空间！",
        "30" => "密码错误",
        "40" => "账号不存在",
        "41" => "余额不足",
        "42" => "帐户已过期",
        "43" => "IP地址限制",
        "50" => "内容含有敏感词"
    );

    /**
     * 发送次数
     * @var int
     */
    private $times=3;

    /**
     * 发送短信有效时间
     * @var int
     */
    private $seconds=86440;

    /**
     * 发送手机验证码
     * use 李耀
     * POST
     * http://www.test.com/sendCode
     * @param $phone
     * @param int $length
     * @return \Illuminate\Http\JsonResponse|int
     */
    public function sendMessage($phone, $length=4){
        #判断手机号是否为空
        if (empty($phone)){
            return response()->json(['code'=>2001,'msg'=>'手机号不能为空','data'=>[]]);
        }
        $send_code=Cache::get('time_'.$phone);
        #判断60s内是否发送过验证码  Sms::verifyCode($param['phone'],$param['code']);
        if (Sms::throttle($send_code['code'])){
            $seconds=60-(time()-$send_code);
            return response()->json([
                'code'=>2001,
                'msg'=>"由于您频繁发送短信验证码,请稍后{$seconds}秒后重试",2001,
                'data'=>[]
            ]);
        }

        $rand=rand(1111,9999);
        $smsapi = "http://api.smsbao.com/";
        $user = "***"; //短信平台帐号
        $pass = md5("****"); //短信平台密码
        $content="【直播电商】项目验证码为{$rand}请勿泄露";//要发送的短信内容
        $phone = "{$phone}";//要发送短信的手机号码
        $sendUrl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
        $result =file_get_contents($sendUrl) ;
        if (env("APP_DEBUG")){
            $result=0;
        }
        if ($result!=0){
            return response()->json(['code'=>2002,
                'msg'=>$this->statusStr[$result],'data'=>[]]);
        }

        Cache::set('code_'.$phone,$rand,60);// 保存验证 set存储  get获取
        Cache::increment('times_'.$phone,1);//increment 自增发送次数
        Cache::set('time_'.$phone,time(),60);// 保存发送的时间

        return  $rand;
    }

    /**
     * 判断当前手机号是否发送过验证码
     * use 李耀
     * POST
     * http://www.test.com/sendCode
     * @param $phone
     * @return bool
     */
    public function throttle($phone){
        # 当前手机号是否发送过
        $current_times = Cache::get('times_'.$phone);
        if (empty($current_times)) {
            // 没有发送过
            Cache::set('times_'.$phone,0,$this->seconds);// 设置发送次数
            return true;
        }
        if ($current_times >= $this->times) {
            return response()->json(['code'=>2001,'msg'=>"发送次数过多、请稍后重试",'data'=>[$current_times]]);
        }
    }

    /**
     * 验证验证码
     * @uses  李耀
     * POST
     * http://www.test.com/sendCode
     * @param $phone
     * @param $code
     * @return \Illuminate\Http\JsonResponse
     */
    public function verifyCode($phone,$code){
        # 验证码验证码 可以封装到基础库里面
        $cache_code = Cache::get('code_'.$phone);
        if (empty($cache_code)) {
            return response()->json(['code'=>2001,'msg'=>'验证码无效','data'=>[]]);
        }
        if ($cache_code != $code) {
            return response()->json(['code'=>2001,'msg'=>'验证码不正确','data'=>[]]);
        } else {
            # 验证通过之后、删除验证码
            Cache::delete('code_'.$phone);
        }
    }

    /**
     * @uses 李耀
     * POST
     * www.test.com/sendCode
     * @param $param
     * @return \Illuminate\Http\JsonResponse
     */
    public function register($param){
        Sms::verifyCode($param['phone'],$param['code']);
        $is_phone=UserSendCode::where('phone',$param['phone'])->first();
        if ($is_phone['status']!=1){ ##
            return response()->json(['code'=>2001,'msg'=>'阅读并接受《用户协议》及《隐私权保护声明》','data'=>[]]);
        }
        if ($is_phone){
            return response()->json(['code'=>2002,'msg'=>'该手机号已被注册、请直接登录','data'=>[]]);
        }
        $data = [
            'phone' => $param['phone'],
            'nickname' => substr_replace($param['phone'],'****',3,4),
            'password' => bcrypt($param['password']),
        ];
        UserSendCode::create($data);
    }
}
